layui.config({
    version: true //一般用于更新模块缓存，默认不开启。设为true即让浏览器不缓存。也可以设为一个固定的值，如：201610
    , debug: false //用于开启调试模式，默认false，如果设为true，则JS模块的节点会保留在页面
    , base: 'js/' //设定扩展的Layui模块的所在目录，一般用于外部模块扩展
}).use(['table', 'layer', 'jquery', 'form', 'laydate', 'base','layedit'], function () {
    var table = layui.table,
        $ = layui.jquery,
        laydate = layui.laydate,
        form = layui.form,
        base = layui.base,
        layedit=layui.layedit,
        layer = layui.layer;
    var tableIns = table.render({
        elem: '#info', limit: 10, cellMinWidth: 80, method: 'get',
        page: {layout: ['count', 'prev', 'page', 'next', 'skip']},
        request: {pageName: "pageNum"},
        url: "customer/list",
        cols: [[ // 表头
            {
                field: 'id', title: '序号', fixed: 'left', width: 60, templet: function (d) {
                    return d.LAY_TABLE_INDEX + 1
                }
            },
            {field: 'customerName', title: '姓名'},
            {
                field: 'sex', title: '性别', templet: function (e) {
                    return e.sex == 1 ? "女" : "男"
                }
            },
            {field: 'email', title: 'email'},
            {field: 'phone', title: '手机号'},
            {field: 'level', title: '客户级别'},
            {
                field: 'createTime', title: '创建时间', templet: function (e) {
                    return base.toDateString(e.createTime)
                }
            },
            {
                field: '', fixed: 'right', title: '操作', templet: function (e) {
                    return '<a class="layui-btn   layui-btn-xs" lay-event="sendEmail">发邮件</a>'
                }
            }]]
    });
    table.on('tool(news)', function (obj) {
        var data = obj.data, layEvent = obj.event;
        //发邮件
        if (layEvent === 'sendEmail') {
            if (data.email == "") {
                layer.msg("客户email不能为空!")
                return
            }
            layer.open({
                title: '发送邮件',
                shadeClose: true,
                shade: false,
                maxmin: true, // 开启最大化最小化按钮
                area: ['800px', '600px'],
                content:'<textarea class="layui-textarea" id="LAY_demo2" style="display: none"></textarea>',
                yes: function () {
                   console.log( layedit.getContent(index))
                    $.ajax({
                        type: "post",
                        url: 'email/send',
                        data: {
                            title:"test",
                            emailContent:layedit.getContent(index),
                            reviceAddr:data.email
                        },
                        success: function () {
                            layer.msg("操作成功!")
                            layer.close(index)
                        }
                    });
                }
            });
            var index = layedit.build('LAY_demo2');
        }
    })

    //新增
    $("#add").click(function () {
        layer.open({
            type: 1,
            title: '新增客户',
            shadeClose: true,
            shade: false,
            maxmin: true, // 开启最大化最小化按钮
            area: ['500px', '300px'],
            content: $("#content").html(),
            btn: ['确定', '取消']
            , yes: function (index, layero) {
                $.ajax({
                    type: "post",
                    url: 'customer/saveOrUpdate',
                    data: base.serializeObject($(".layui-layer-content .layui-form")),
                    success: function () {
                        layer.msg("操作成功!")
                        layer.close(index)
                        base.reload(tableIns)
                    }
                });
            }, btn2: function (index, layero) {
                layer.close(index)
            }
        });
        form.render(null, "form");
    });


})